package com.example.demo.service;

import com.example.demo.dto.*;
import com.example.demo.mode.PrintFile;
import com.example.demo.mode.PrintFileOperation;
import com.example.demo.mode.PrintFilesSub;
import com.example.demo.repository.PrintFileMapper;
import com.microsoft.sqlserver.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

/**
 * @author 21024
 */
@Service
public class PrintFileService {
    /**
     * 用于验证品号的字母数字模式
     *
     * <p>匹配规则：仅包含数字0-9、小写字母a-z和大写字母A-Z</p>
     * <p>示例：</p>
     * <ul>
     *   <li>"ABC123" - 匹配</li>
     *   <li>"abc_123" - 不匹配（包含下划线）</li>
     *   <li>"中文" - 不匹配</li>
     * </ul>
     */
    private static final Pattern ALPHANUMERIC_PATTERN = Pattern.compile("^[0-9a-zA-Z]+$");
    private final PrintFileMapper printFileMapper;

    @Autowired
    public PrintFileService(PrintFileMapper printFileMapper) {
        this.printFileMapper = printFileMapper;
    }


    public List<PrintFileListDto> printFileList() {
        return printFileMapper.printFileList();
    }

    public PrintFileDto selectByPartNumber(String partNumber) {
        PrintFileDto printFileDto = printFileMapper.selectByPartNumber(partNumber);
        printFileDto.setPrintFilesSubList(printFileMapper.selectByPartNumberSub(partNumber));
        return printFileDto;
    }

    public int selectPartNumber(String partNumber) {
        return printFileMapper.selectPartNumber(partNumber);
    }

    public Boolean update(PrintFile printFile, String clientIp, StringBuilder fileNameListBuilder) {
        PrintFileOperation printFileOperation = new PrintFileOperation();
        if (Boolean.TRUE.equals(printFileMapper.update(printFile))) {
            printFileOperation.setFileId(printFile.getFileId());
            printFileOperation.setFileName(fileNameListBuilder.toString());
            printFileOperation.setUserId(printFile.getLastUpdateUserId());
            printFileOperation.setOperationType(Short.valueOf("2"));
            printFileOperation.setOperationTime(printFile.getLastUpdateTime());
            printFileOperation.setIpAddress(clientIp);
            printFileOperation.setRemarks(printFile.getDescription());
            printFileOperation.setUserName(printFile.getLastUpdateUserName());
            return printFileMapper.insertOperation(printFileOperation);
        } else {
            return false;
        }
    }

    public Boolean insert(NewPrintFileDto printFile, String clientIp, StringBuilder fileNameListBuilder) {
        PrintFileOperation printFileOperation = new PrintFileOperation();
        printFileMapper.insert(printFile);
        for (PrintFilesSub printFilesSub : printFile.getPrintFilesSubList()) {
            printFilesSub.setFileId(printFile.getFileId());
            printFileMapper.insertSub(printFilesSub);
        }
        printFileOperation.setFileId(printFile.getFileId());
        printFileOperation.setFileName(fileNameListBuilder.toString());
        printFileOperation.setUserId(Integer.parseInt(printFile.getCreateUserId()));
        printFileOperation.setOperationType(Short.valueOf("1"));
        printFileOperation.setOperationTime(printFile.getCreateTime());
        printFileOperation.setIpAddress(clientIp);
        printFileOperation.setRemarks(printFile.getDescription());
        printFileOperation.setUserName(printFile.getCreateUserName());
        return printFileMapper.insertOperation(printFileOperation);
    }

    public List<PrintFileListDto> findPrintFileList(String condition) {
        if (StringUtils.isEmpty(condition)) {
            return printFileMapper.printFileList();
        }

        return ALPHANUMERIC_PATTERN.matcher(condition).matches() ?
                printFileMapper.findByPartNumber(condition) :
                printFileMapper.findByFileName(condition);
    }

    public void getDownloadFile(PrintDownloadUserDto printDownloadUserDto, String clientIp) {
        PrintFile printFile = printFileMapper.selectByFileName(printDownloadUserDto.getFileName());
        PrintFileOperation printFileOperation = new PrintFileOperation();
        printFileOperation.setFileId(printFile.getFileId());
        printFileOperation.setFileName(printDownloadUserDto.getFileName());
        printFileOperation.setUserId(printDownloadUserDto.getWorkCode());
        printFileOperation.setOperationType(Short.valueOf("0"));
        printFileOperation.setOperationTime(printDownloadUserDto.getTime());
        printFileOperation.setIpAddress(clientIp);
        printFileOperation.setRemarks(printFile.getDescription());
        printFileOperation.setUserName(printDownloadUserDto.getUserName());
        printFileMapper.insertOperation(printFileOperation);
    }

    public Boolean updatePrintFilesSub(String fileName, String partNumber, String subPartName) {
        return printFileMapper.updateSub(fileName, partNumber, subPartName);
    }

    public Boolean insertPrintFilesSub(PrintFilesSub printFilesSub) {
        return printFileMapper.insertSub(printFilesSub);
    }

    public List<DownloadPrintFileListDto> downloadPrintFileList(String partNumber) {
        return printFileMapper.downloadPrintFileList(partNumber);
    }
}
